package com.hexorld.core;

import android.content.Context;
import android.graphics.Canvas;

import com.hexorld.layer.ActionLayer;
import com.hexorld.layer.BgLayer;
import com.hexorld.layer.FrontLayer;
import com.hexorld.layer.Layer;
import com.hexorld.utils.Constants;
import com.hexorld.utils.Coordinate;

public class GameBoardView {
	private LayerManager mLayerManager = null;
	private SpriteManager mSpriteManager = null;
	private Context mContext;
	public GameBoardView(Context context){
		mContext = context;
		mLayerManager = new LayerManager();
		mSpriteManager = new SpriteManager(mLayerManager);
		initLayers();
		
		test(); //FOR TEST ONLY
	}
	
	//FOR TEST ONLY
	Character char1, char2;
	int count = 0;
	void test(){
		char1 = new Monster(mContext, 30, 50, 0);
		char1.setCoordinate(new Coordinate(count, 2, 0));
		ObjectStatus status = new ObjectStatus();
		status.hp = 33;
		status.maxHp = 100;
		status.sp = 67;
		status.maxSp = 82;
		char1.setStatus(status);
		mSpriteManager.addSprite(char1);
		
		char2 = new Monster(mContext, 30,50,1);
		char2.setCoordinate(new Coordinate(2,count,0));
		status = new ObjectStatus();
		status.hp = 88;
		status.maxHp = 100;
		status.sp = 32;
		status.maxSp = 86;
		char2.setStatus(status);
		mSpriteManager.addSprite(char2);
	}
	
	private void initLayers(){
		//Background layer
		Layer layer = new BgLayer(mContext, Constants.GAME_BOARD_WIDTH, Constants.GAME_BOARD_HEIGHT, 0);
		mLayerManager.addLayer(layer.Z, layer);
		
		//Action layer for dynamic object
		layer = new ActionLayer(mContext, Constants.GAME_BOARD_WIDTH, Constants.GAME_BOARD_HEIGHT, 1);
		mLayerManager.addLayer(layer.Z, layer);
		
		//Front layer for something should display before Action layer
		layer = new FrontLayer(mContext, Constants.GAME_BOARD_WIDTH, Constants.GAME_BOARD_HEIGHT, 2);
		mLayerManager.addLayer(layer.Z, layer);
	}
	
	public void draw(Canvas canvas) {
		if(canvas == null || mLayerManager == null || mContext == null)
			return;
		mLayerManager.updateAll(mContext, canvas);
	}
	
	
	/**
	 * update all the model, physics, time
	 * then call draw()
	 */
	public void update(){
		//for test only
		if(char1 == null || char2 == null)
			return;
		char1.setCoordinate(new Coordinate(count, 2, 0));
		char2.setCoordinate(new Coordinate(2,count++,0));
		
		if(count > 18)
			count =0;
	}
}
